//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include "Common.h"
#include "CPropVNC.hpp"
#include "VNC_Password.h"

const LPWSTR szRegKeyVNC    = TEXT("Software\\RealVNC\\WinVNC4");
const LPWSTR szRegValRunVNC = TEXT("runvnc");
const LPWSTR szRegValPWD    = TEXT("password");

CPropVNC::CPropVNC (CTerminalPropSheet *pOwner) : CTerminalPropPage(pOwner)
{
	Init (IDD_PROPPAGE_VNC);
}

CPropVNC::~CPropVNC()
{
}

LRESULT CPropVNC::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CTerminalPropPage::OnInitDialog (uMsg, wParam, lParam, bHandled);

	m_remoteCtrlWin.Attach(GetHwnd(),IDC_REMOTE_CTRL);

	m_pwd1Win.Attach(GetHwnd(),IDC_PWD1);
	m_pwd2Win.Attach(GetHwnd(),IDC_PWD2);

	m_remoteCtrlWin.CheckButton(m_remoteCtrl);

	m_pwd1Win.EnableWindow(m_remoteCtrl);
	m_pwd2Win.EnableWindow(m_remoteCtrl);
	
	m_pwd1Win.SetWindowText(TEXT("----------"));
	m_pwd2Win.SetWindowText(TEXT("----------"));
	m_pwdWasChanged = FALSE;

	return 1;
}


LRESULT CPropVNC::OnSetActive (int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	return 0;
}


LRESULT CPropVNC::OnEnableRemoteControl (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{

	BOOL state = m_remoteCtrlWin.GetButtonState();

	m_pwd1Win.EnableWindow(state);
	m_pwd2Win.EnableWindow(state);

	GetOwner().SetReboot(TRUE);

	return 0;
}

LRESULT CPropVNC::OnPwdChange (WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{	
	m_pwdWasChanged = TRUE;
	m_pwd2Win.SetWindowText(TEXT(""));

	GetOwner().SetReboot(TRUE);

	return 0;
}

LRESULT CPropVNC::OnFocus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	m_pwd1Win.SetWindowText(TEXT(""));
	return 0;
}

BOOL CPropVNC::ReadRegistrySettings()
{
	DWORD dwSize;
	DWORD dwType;
	LONG  lResult;
	HKEY  hkey;

	char bytes[100];

	m_remoteCtrl    = FALSE;
	m_pwdWasChanged = FALSE;

	lResult = RegOpenKeyEx( HKEY_CURRENT_USER , szRegKeyVNC, NULL, KEY_READ, &hkey);

	if (lResult == ERROR_SUCCESS) 
	{
		dwSize  = sizeof(m_remoteCtrl);
		RegQueryValueEx(hkey, szRegValRunVNC, NULL, &dwType, (PBYTE)&m_remoteCtrl, &dwSize); 	   
		RegCloseKey(hkey);
	}

	return TRUE;
}

BOOL CPropVNC::WriteRegistrySettings()
{
	LONG  lResult;
	HKEY  hkey;
	BOOL state;

	lResult = RegOpenKeyEx(HKEY_CURRENT_USER, szRegKeyVNC, NULL, KEY_WRITE, &hkey );
	if ( lResult == ERROR_SUCCESS )
	{
		state = m_remoteCtrlWin.GetButtonState();
		RegSetValueEx(hkey, szRegValRunVNC, NULL, REG_DWORD ,(PBYTE)&state , sizeof(state));

		if ( m_pwdWasChanged == TRUE )
		{
			TCHAR pwd1[10] = {0};
			char * pwd = new char[10];

			m_pwd1Win.GetWindowText(pwd1,10);
			wcstombs(pwd, pwd1,m_pwd1Win.GetWindowTextLength()*sizeof(TCHAR)); 
  
			rfb::PlainPasswd password((char *)pwd);
			rfb::ObfuscatedPasswd obfPwd(password);

			RegSetValueEx(hkey, szRegValPWD, NULL, REG_BINARY,(PBYTE)obfPwd.buf, obfPwd.length);
		}

		
		RegCloseKey(hkey);
	}

	return TRUE;
}


BOOL CPropVNC::StoreData()
{
	return TRUE;
}

BOOL CPropVNC::Validate()
{
	TCHAR szDesc [MAX_PATH] = {0};
	TCHAR szTitle[MAX_PATH] = {0}; 
	
	TCHAR pwd1[10] = {0};
	TCHAR pwd2[10] = {0};

	if( m_remoteCtrlWin.GetButtonState() == FALSE)
	{
		return TRUE;
	}
	
	if ( m_pwdWasChanged == FALSE )
	{
		return TRUE;
	}

	m_pwd1Win.GetWindowText(pwd1,10);
	m_pwd2Win.GetWindowText(pwd2,10);
	
	if ( _tcscmp(pwd1, pwd2) != 0 )
	{
		LoadString (ghInstance, IDS_TITLE_WRONGPWD, szTitle, MAX_PATH);
		LoadString (ghInstance, IDS_PWD_NOT_MATCH , szDesc , MAX_PATH);
		MessageBox(szDesc, szTitle, MB_OK|MB_ICONERROR);
		
		m_pwd1Win.SetFocus();
		return FALSE;
	}

	if ( m_pwd1Win.GetWindowTextLength() == 0 )
	{
		LoadString (ghInstance, IDS_TITLE_WRONGPWD, szTitle, MAX_PATH);
		LoadString (ghInstance, IDS_PWD_EMPTY     , szDesc , MAX_PATH);
		MessageBox(szDesc, szTitle, MB_OK|MB_ICONERROR);

		m_pwd1Win.SetFocus();
		return FALSE;
	}

	if ( m_pwd1Win.GetWindowTextLength() > 8 )
	{
		LoadString (ghInstance, IDS_TITLE_WRONGPWD, szTitle, MAX_PATH);
		LoadString (ghInstance, IDS_PWD_TOLONG    , szDesc , MAX_PATH);
		MessageBox(szDesc, szTitle, MB_OK|MB_ICONERROR);

		m_pwd1Win.SetFocus();
		return FALSE;
	}

	return TRUE;
}
